

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.processes.threading.thread &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.processes" href="../../processes.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../processes.html" accesskey="U">pyamp.processes</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.processes.threading.thread</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The thread module contains the Thread class which provides the ability to</span>
<span class="sd">create a threaded class. The Thread class manages starting the thread, and</span>
<span class="sd">shutting it down appropriately. The subclasses Thread will can be notified when</span>
<span class="sd">the thread starts, is shut down, or at each cycle so it can perform some</span>
<span class="sd">process.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">from</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="n">stderr</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">sleep</span>

<span class="kn">from</span> <span class="nn">pyamp.logging</span> <span class="kn">import</span> <span class="n">Loggable</span>
<span class="kn">from</span> <span class="nn">pyamp.util</span> <span class="kn">import</span> <span class="n">getStackTrace</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Cycle&quot;</span><span class="p">,</span> <span class="s">&quot;Thread&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Thread"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread">[docs]</a><span class="k">class</span> <span class="nc">Thread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">,</span> <span class="n">Loggable</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The Thread class provides a base class for creating threaded classes. It</span>
<span class="sd">    is essentially a wrapper for the :class:`threading.Thread` class to make</span>
<span class="sd">    threaded classes easier to manage.</span>

<span class="sd">    It provides the following functions that can be overridden by subclasses:</span>

<span class="sd">    * onStart(self) -- This function is called when the Thread is started.</span>

<span class="sd">    * onException(self, e, trace) -- This function is called in the event that</span>
<span class="sd">                                   an exception is encountered while the</span>
<span class="sd">                                   Thread is cycling.</span>

<span class="sd">    * onCycle(self, increment) -- This function is called once each time the</span>
<span class="sd">                                Thread cycles.</span>

<span class="sd">    * onShutdown(self) -- This function is called in the event that the Thread</span>
<span class="sd">                          is shutdown.</span>

<span class="sd">    Once the Thread has started it continues to run until the :func:`shutdown`</span>
<span class="sd">    function is called, or until the condition function returns False.</span>

<span class="sd">    Subclasses can set the Period property which sets the number of seconds</span>
<span class="sd">    that the Thread will sleep at the end of each cycle. The default value is</span>
<span class="sd">    one millisecond.</span>

<span class="sd">    This class implements the :class:`pyamp.logging.Loggable` interface.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="c"># This property sets the number of seconds to sleep at the end of</span>
    <span class="c"># each cycle</span>
    <span class="n">Period</span> <span class="o">=</span> <span class="mf">0.01</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conditionFn</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * conditionFn -- A function that determines when the Thread should exit</span>
<span class="sd">        * logger -- The logger for this class</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">Loggable</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__conditionFn</span> <span class="o">=</span> <span class="n">conditionFn</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">False</span>

<div class="viewcode-block" id="Thread.run"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Run the Thread until it is shut down or its condition function</span>
<span class="sd">        returns False.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">True</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">onStart</span><span class="p">()</span>

        <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="c"># Continue until the thread is shut down, or the condition</span>
        <span class="c"># function returns False</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">__conditionFn</span><span class="p">():</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">onCycle</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">onException</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">getStackTrace</span><span class="p">())</span>

            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">sleep</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">Period</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">onStop</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="Thread.shutdown"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.shutdown">[docs]</a>    <span class="k">def</span> <span class="nf">shutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Shutdown the Thread.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">onShutdown</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">False</span>
</div>
<div class="viewcode-block" id="Thread.isRunning"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.isRunning">[docs]</a>    <span class="k">def</span> <span class="nf">isRunning</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if the Thread is still running.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span>

    <span class="c">##### Functions to be overridden by concrete Threads #####</span>
</div>
<div class="viewcode-block" id="Thread.onException"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.onException">[docs]</a>    <span class="k">def</span> <span class="nf">onException</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">trace</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;The Thread encountered an exception.</span>

<span class="sd">        .. note:: This function should be overridden by concrete Threads.</span>

<span class="sd">        * e -- The exception</span>
<span class="sd">        * trace -- The traceback for the exception</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">stderr</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">trace</span>
</div>
<div class="viewcode-block" id="Thread.onStart"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.onStart">[docs]</a>    <span class="k">def</span> <span class="nf">onStart</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This method is called when the Thread is started and should</span>
<span class="sd">        be overridden by subclasses.</span>

<span class="sd">        .. note:: This function should be overridden by concrete Threads.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>
</div>
<div class="viewcode-block" id="Thread.onCycle"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.onCycle">[docs]</a>    <span class="k">def</span> <span class="nf">onCycle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">increment</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This method is called when at each cycle of the Thread and should</span>
<span class="sd">        be overridden by subclasses.</span>

<span class="sd">        .. note:: This function should be overridden by concrete Threads.</span>

<span class="sd">        * increment -- The current increment number</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>
</div>
<div class="viewcode-block" id="Thread.onShutdown"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.onShutdown">[docs]</a>    <span class="k">def</span> <span class="nf">onShutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This method is called when the Thread is shut down and should</span>
<span class="sd">        be overridden by subclasses.</span>

<span class="sd">        .. note:: This function should be overridden by concrete Threads.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>
</div>
<div class="viewcode-block" id="Thread.onStop"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Thread.onStop">[docs]</a>    <span class="k">def</span> <span class="nf">onStop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;This method is called when the Thread is stopped.</span>

<span class="sd">        .. note:: This function should be overridden by concrete Threads.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span>

</div></div>
<div class="viewcode-block" id="Cycle"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Cycle">[docs]</a><span class="k">class</span> <span class="nc">Cycle</span><span class="p">(</span><span class="n">Loggable</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The Cycle class provides a class which will continue cycling until</span>
<span class="sd">    a condition is no longer True.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conditionFn</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * conditionFn -- The condition function</span>
<span class="sd">        * logger -- The logger object</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Loggable</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">logger</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__conditionFn</span> <span class="o">=</span> <span class="n">conditionFn</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">False</span>

<div class="viewcode-block" id="Cycle.start"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Cycle.start">[docs]</a>    <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Start cycling until the condition function returns False.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">__conditionFn</span><span class="p">()</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyboardInterrupt</span><span class="p">,</span> <span class="ne">SystemExit</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">False</span>

        <span class="k">return</span> <span class="bp">True</span>
</div>
<div class="viewcode-block" id="Cycle.shutdown"><a class="viewcode-back" href="../../../../_generated/pyamp_processes_threading_thread.html#pyamp.processes.threading.thread.Cycle.shutdown">[docs]</a>    <span class="k">def</span> <span class="nf">shutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Stop cycling.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__shouldRun</span> <span class="o">=</span> <span class="bp">False</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../../processes.html" >pyamp.processes</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>